package com.project.slsx.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.project.slsx.Constant.Constans;
import com.project.slsx.R;
import com.project.slsx.adapter.StorageAdapter;
import com.project.slsx.bean.StorageBean;
import com.project.slsx.utils.DialogUtils;
import com.project.slsx.utils.ParamsUtils;

/**
 * 入库 搜索
 */
public class StorageActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = StorageActivity.class.getSimpleName();
    private EditText edSerarch;
    private Button btnSearch;
    private Button btnInStorgae;
    private String searchStr;
    private StorageAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_storage;
    }

    @Override
    public void initView() {
        TextView title = findViewById(R.id.tv_title);
        title.setText("入库");
        findViewById(R.id.rl_back).setOnClickListener(v -> finish());

        edSerarch = findViewById(R.id.ed_storage_search);
        btnSearch = findViewById(R.id.btn_storage_search);
        btnInStorgae = findViewById(R.id.btn_storage_in);
        edSerarch.addTextChangedListener(new MyWatcher());
        RecyclerView rv = findViewById(R.id.rv_storeage);
        rv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new StorageAdapter(R.layout.layout_item_stoege);
        rv.setAdapter(adapter);

        btnSearch.setOnClickListener(this);
        btnInStorgae.setOnClickListener(this);
        findViewById(R.id.btn_storage_total_list).setOnClickListener(this);
    }

    @Override
    public void initData() {
        adapter.setOnItemClickListener((adapter, view, position) -> {
            StorageBean.ResultBean resultBean = (StorageBean.ResultBean) adapter.getData().get(position);
            Intent intent = new Intent(mContext, StorageInterfaceActivity.class);
            intent.putExtra("storebean", resultBean);
            startActivity(intent);
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_storage_search:
                if (TextUtils.isEmpty(searchStr)) {
                    showToast(mContext, "请输入搜索商品");
                    return;
                }
                doSerach();
                break;

            case R.id.btn_storage_in:
                Intent intent = new Intent(mContext, StorageList1Activity.class);
                startActivity(intent);
                break;

            case R.id.btn_storage_total_list:
                startActivity(new Intent(mContext,StoreageTotalListActivity.class));
                break;
        }
    }

    /**
     * 搜索商品
     */
    private void doSerach() {
        DialogUtils.showLoading(getSupportFragmentManager());
        HttpParams storagesearchParams = ParamsUtils.getStoragesearchParams();
        storagesearchParams.put("keyword", searchStr);
        OkGo.<String>post(Constans.BASEURL).tag("storagesearch").params(storagesearchParams).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                Log.i(TAG, "onSuccess: " + response.body());
                StorageBean storageBean = JSONObject.parseObject(response.body(), StorageBean.class);
                if (storageBean.getErrno() == Constans.SUCCESS) {
                    if (storageBean.getResult() != null && storageBean.getResult().size() > 0) {
                        adapter.setNewData(storageBean.getResult());
                        showToast(mContext, storageBean.getErrmsg());
                    } else {
                        showToast(mContext, "无数据");
                    }
                } else {
                    showToast(mContext, storageBean.getErrmsg());
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                showToast(mContext, "error");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUtils.dissmissDialog();
            }
        });
    }

    private class MyWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            searchStr = s.toString().trim();
        }
    }

}
